package py.com.fms.fms.session.maintenances;

//External classes imports 
//Java Classes
import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

//Seam Classes
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage.Severity;
import org.jboss.seam.security.Credentials;
import org.jboss.seam.security.Identity;

//project classes imports
import py.com.fms.fms.bean.RolesBean;

import py.com.fms.fms.persistence.services.RolesServices;

import py.com.fms.exception.CommonException;
import py.com.fms.session.ServiceSqlMap;
import py.com.fms.session.WithinHolder;
import py.com.fms.utilities.*;

@SuppressWarnings("unused")
@Name("editRole")
@Scope(ScopeType.SESSION)
public class EditRole {
	private FacesMessages facesMessages;
	@Logger
	private Log log;
	@In("#{serviceSqlMap}")
	private ServiceSqlMap sqlMap;
	private String initVar;
	private Date today = null;
	
	// Task Attendees Attributes 
	
	@In("#{withinHolder}")
	private WithinHolder aid;
	@In("#{convertAssist}")
	private ConvertAssistant convert;
	
	private DateAssistant dateAssist;

	// Attribute used for saving and deleting
	private RolesBean role;
	
	// Services
	private RolesServices rlSrv;
	
	// Collection of monedasBean
	private Collection<RolesBean> allRoles;

	// Attribute used for editing or updating
	private RolesBean editRole;
	
//	Necessary Attributes;
	
	

	/**
	 * [[[[[[[[[[[[ Methods ]]]]]]]]]]]] 
	 * [[[[[[[[[[-------------]]]]]]]]]]
	 * @throws CommonException 
	 */

	// Used to initialize information from a variable
	public void initData() throws CommonException {

		System.out.println("Initialization Flag = " + this.getInitVar());

		if ("S".equals(this.getInitVar())) {
			this.initBeans();
			this.initRoles();
			this.initAssistants();
			this.initCollections();
			this.loadDataGrid();
			
		}
		this.setInitVar(null);
		System.out.println("OUT");
				
	}
	
	public void initBeans(){
		
		if (role == null){
			role = new RolesBean();
			role.setStatus(true);
		}
		
		if (editRole == null){
			editRole = new RolesBean();
		}
	}
	
	public void initRoles() throws CommonException{
		
		if (rlSrv == null){
			rlSrv = new RolesServices(this.sqlMap.getConfig());
		}		
				
	}
	
	public void initAssistants(){
		
		
		if(aid == null){
			aid = new WithinHolder();
		}
		
		if(convert == null){
			convert = new ConvertAssistant();
		}
		
		if(dateAssist == null){
			dateAssist = new DateAssistant();
		}
		
	}
	
	public void initCollections(){
		if(allRoles == null){
			allRoles = new ArrayList<RolesBean>();
		}
		
	

	}
	
	public void cleanData(){

			role = null;

			editRole = null;

			rlSrv = null;
			
			aid = null;

			convert = null;

			dateAssist = null;

			allRoles = null;

		
	}

	// Used to load a grid using a collection
	private void loadDataGrid() {
		try {
			this.setAllRoles(rlSrv.getAllRoles());
			for (RolesBean role : this.getAllRoles()) {
				System.out.println("Role: " + role.getDescrip());
			}
		} catch (CommonException e) {
			e.printStackTrace();
		}
	}

	// Used to cancel
	public String cancelData() throws CommonException {
//		this.facesMessages.clear();
		this.setInitVar("S");
		this.cleanData();
		this.initData();
		return null;
	}
		

	// Used for data saving process
	public void saveNewData() throws ParseException {
		try {
//			this.facesMessages.clear();
			RolesBean newRole = new RolesBean();

			aid = new WithinHolder();
			this.today = DateAssistant.getCurrentDateTime();
			Long user = new Long(1);
			
			if (this.getRole() == null) {
				System.out.println("Roles Entity is null");
				throw new CommonException("Roles Entity is null");
			}
			System.out.println("BEFORE SETTING VALUES");
			
			// Setting field values
			newRole.setDescrip(convert.convertField(this.getRole().getDescrip()));
			newRole.setStatus(this.getRole().getStatus());
			newRole.setStartDate(this.today);
			newRole.setLastUpdDate(this.today);
			System.out.println("Today: " + this.today);
			newRole.setUserIns(user);

			rlSrv.saveNewRole(newRole);
			
			// Initializing field values
			newRole = null;
			
			

		} catch (CommonException e) {
//			this.facesMessages.add(Severity.ERROR, e.getMessage());
			System.out.println("NO SE PUDO INSERTAR. " + e.getMessage());
			log.error(e);
		}finally{
			this.setRole(null);
			// Setting Initialization VAR
			this.setInitVar("S");
		}

	}
	
	// Retrieving data for updating
	public void dataToEdit(Long id) throws CommonException{
		System.out.println("id = " + id);
		for(RolesBean bean : this.getAllRoles()){
			if(bean.getId().equals(id)){
				System.out.println(bean.getId() + " = " + id);
				this.setEditRole(bean);
			}else{
				System.out.println("Not Coincidences");
			}
		}	
	}
	
	public void dataToEditUpd() throws ParseException{
		try {
			// SETTING EXTERNAL VALUES
			this.today = DateAssistant.getHoleDateTime();
			
			// UPDATING EXTERNAL VALUES
			this.getEditRole().setLastUpdDate(this.today);
			
			// SAVING VALUES
			rlSrv.updateRole(this.getEditRole());
		} catch (CommonException e) {
//			this.facesMessages.add(Severity.ERROR, e.getMessage());
			System.out.println("NO SE PUDO ACTUALIZAR EL REGISTRO. " + e.getMessage());
			log.error(e);
		}
	}
	
	public void dataToDelete(Long id){
		for (RolesBean bean: this.getAllRoles()){
			if (bean.getId() == id) {
				try {
					rlSrv.deleteRoleById(bean.getId());
					
				} catch (Exception e) {
//					this.facesMessages.add(Severity.ERROR, e.getMessage());
					System.out.println("NO SE BORRAR EL REGISTRO. " + e.getMessage());
					log.error(e);
				}
			}
			
		}
		this.setInitVar("S");
	}

	// GETTERS AND SETTERS
	public FacesMessages getFacesMessages() {
		return facesMessages;
	}

	public void setFacesMessages(FacesMessages facesMessages) {
		this.facesMessages = facesMessages;
	}

	public Log getLog() {
		return log;
	}

	public void setLog(Log log) {
		this.log = log;
	}

	public ServiceSqlMap getSqlMap() {
		return sqlMap;
	}

	public void setSqlMap(ServiceSqlMap sqlMap) {
		this.sqlMap = sqlMap;
	}

	public String getInitVar() {
		return initVar;
	}

	public void setInitVar(String initVar) {
		this.initVar = initVar;
	}

	public RolesBean getRole() {
		return role;
	}

	public void setRole(RolesBean role) {
		this.role = role;
	}

	public Collection<RolesBean> getAllRoles() {
		return allRoles;
	}

	public void setAllRoles(Collection<RolesBean> allRoles) {
		this.allRoles = allRoles;
	}

	public RolesBean getEditRole() {
		return editRole;
	}

	public void setEditRole(RolesBean editRole) {
		this.editRole = editRole;
	}
	
	
}
